package logic

import (
	"box-fresh/model/cache"
	"box-fresh/model/mysql"
	"context"
	"encoding/json"
	"time"

	"box-fresh/hemarpc/hemarpc"
	"box-fresh/hemarpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HeListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHeListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HeListLogic {
	return &HeListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HeListLogic) HeList(in *hemarpc.HeListRequest) (*hemarpc.HeListResponse, error) {
	// todo: add your logic here and delete this line
	var hes mysql.HesFen
	var lists []*hemarpc.HeList
	redisData, _ := cache.Rdb.Get(cache.Ctx, "hesList").Result()
	if redisData == "" {
		var err error
		lists, err = hes.HesFenList(mysql.DB, lists, in)
		if err != nil {
			return nil, err
		}
		jsonData, _ := json.Marshal(lists)
		cache.Rdb.Set(cache.Ctx, "hesList", jsonData, time.Hour*30)
	} else {
		json.Unmarshal([]byte(redisData), &lists)
	}

	return &hemarpc.HeListResponse{
		Lists: lists,
	}, nil
}
